package yxk.wtc.wtom.business;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.db.DBRoute;
import kd.bos.exception.KDException;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.schedule.executor.AbstractTask;
import kd.hr.hbp.business.servicehelper.HRBaseServiceHelper;
import kd.hr.hbp.common.util.HRDBUtil;
import kd.hr.hbp.common.util.HRStringUtils;
import kd.wtc.wtbs.common.constants.WTCBillConstans;
import kd.wtc.wtbs.common.constants.WTCCommonConstants;
import kd.wtc.wtbs.common.constants.relate.WTCBaseConstants;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class OtChangeBillTransNormal extends AbstractTask {
    private static final Log LOG = LogFactory.getLog(OtChangeBillTransNormal.class);


    @Override
    public void execute(RequestContext requestContext, Map<String, Object> map) throws KDException {
        LOG.info("OtChangeBillTransNormal start");
        HRBaseServiceHelper serviceHelper = new HRBaseServiceHelper("wtom_otbillchange");
        QFilter qFilterChange = new QFilter("ischange", QCP.equals, Boolean.TRUE);
        QFilter auditPass = new QFilter(WTCBaseConstants.BILLSTATUS, QCP.equals, WTCCommonConstants.AUDITSTATUS_DISCARD);
        QFilter keyColumn = new QFilter("inputdevicetype", QCP.not_equals, " ");
        DynamicObject[] changeBillArr = serviceHelper.loadDynamicObjectArray(new QFilter[]{qFilterChange, keyColumn,auditPass});
        List<DynamicObject> transList = Lists.newArrayListWithExpectedSize(changeBillArr.length);
        for (DynamicObject dynamicObject : changeBillArr) {
            String inputdevicetype = dynamicObject.getString("inputdevicetype");
            if(inputdevicetype != null && inputdevicetype.contains(WTCBaseConstants.BILLSTATUS)){
                transList.add(dynamicObject);
            }
        }
        LOG.info("OtChangeBillTransNormal changeSize = {}",transList.size());
        if (transList.isEmpty()) {
            LOG.info("OtChangeBillTransNormal end");
            return;
        }
        for (DynamicObject changeBill : transList) {
            // 反序列化
            Map<String,String> info = JSON.parseObject(changeBill.getString("inputdevicetype"),Map.class);
            if(info.get(WTCBaseConstants.BILLSTATUS) != null){
                changeBill.set(WTCBaseConstants.BILLSTATUS,info.get(WTCBaseConstants.BILLSTATUS));
            }
            if(info.get(WTCBillConstans.FIELD_AUDITSTATUS) != null){
                changeBill.set(WTCBillConstans.FIELD_AUDITSTATUS,info.get(WTCBillConstans.FIELD_AUDITSTATUS));
            }
        }
        serviceHelper.save(transList.toArray(new  DynamicObject[]{}));
        // 更新时间和修改人
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (DynamicObject changeBill : transList) {
            long id = changeBill.getLong(WTCBaseConstants.ID);
            if (id == 0) {
                continue;
            }
            // 反序列化
            Map<String,String> info = JSON.parseObject(changeBill.getString("inputdevicetype"),Map.class);
            String modifyTime = "";
            if(info.get(WTCBaseConstants.MODIFYTIME) != null){
                Date modifyDate = new Date(Long.parseLong(info.get(WTCBaseConstants.MODIFYTIME)));
                modifyTime = "{ts'"+simpleDateFormat.format(modifyDate)+"'}";
            }
            String modifierStr = "";
            if (info.get(WTCBaseConstants.MODIFIER) != null) {
                modifierStr =  Long.parseLong(info.get(WTCBaseConstants.MODIFIER))+"";
            }
            DBRoute dbRoute = new DBRoute("wtc");
            if (HRStringUtils.isNotEmpty(modifyTime) && HRStringUtils.isNotEmpty(modifierStr)) {
                HRDBUtil.execute(dbRoute, "update t_wtom_otapply set fmodifytime = " + modifyTime + " , fmodifierid = " + modifierStr + "  where fid = " + id, null);
            }else if (HRStringUtils.isNotEmpty(modifyTime)) {
                HRDBUtil.execute(dbRoute, "update t_wtom_otapply set fmodifytime = " + modifyTime + "  where fid = " + id, null);
            }
            if (HRStringUtils.isNotEmpty(modifierStr)) {
                HRDBUtil.execute(dbRoute, "update t_wtom_otapply set  fmodifierid = " + modifierStr + "  where fid = " + id, null);
            }

        }

        LOG.info("OtChangeBillTransNormal end");
    }
}
